package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.CommercialVehicle;

/**
 * @author zhangxueliang
 * 车辆名称三级联动 （第三级）
 */
public class CarLoadCarStyleAjaxAction extends TmsAjaxAction {

	private Logger logger = Logger.getLogger(CarLoadCarStyleAjaxAction.class);
	private static final String BRANDID = "brandId";
	private static final String CARSTYLEID="carStyleId";
	private static final String TYPE="type";
	
	@SuppressWarnings("unchecked")
	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc)throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		String brandId = request.getParameter(BRANDID);
		String  carStyleId= request.getParameter(CARSTYLEID);
		String fromType = request.getParameter(TYPE);
		List list = new ArrayList();
		if("1".equals(fromType)){
			CarStyle carStyle = CarStyle.getInstance();	
			list = carStyle.getAllCarStyleInfo(brandId);
		}else if("2".equals(fromType)){
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			list = commercialVehicle.getAllStyleInfo(brandId);// 车主类型list
		}
		
		
		//拼select
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"carStyle\" class=\"exfasitem_selet requirecs\" id=\"carStyle\" onchange=\"getCarStyleId(this.value);\">");
		Map map = null;
		int j=0;
		for(int i=0;i<list.size();i++)
		{
			
			if(j==0){
				str.append("<option value=\""+""+"\">"+"款式"+"</option>");
			}
		map =(Map)list.get(i);
		
			
			//设置默认参数
			if(map.get("carStyleId").equals(carStyleId)){
				str.append("<option value='");
				str.append(map.get("carStyleId"));
				str.append("'");
				str.append(" selected='");
				str.append("true'");
				str.append(" >");
				str.append(map.get("carStyleName"));
				str.append("</option>");
			
			}else{
			str.append("<option value=\""+map.get("carStyleId")+"\">"+map.get("carStyleName")+"</option>");
			}
			j++;
		}
		str.append("</select>");
		
		String responseText =str.toString();
		controller.commit();
		responseValues(response, responseText, TEXT);
		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}

}
